// This do-file extracts an index of democracy from V-Dem

use "$project_path/data/1_input/elections/V-Dem/vdem_cy_extract.dta", clear

keep country_name year v2x_polyarchy v2x_liberal v2x_partip v2xdl_delib v2x_egal v2x_jucon v2xlg_legcon v2elparlel v2x_regime v2exdfdshs_ord v2exdfcbhs_ord v2exdfdmhs_ord v2exdfvths_ord v2exdfpphs_ord v2exdjdshg_ord v2exdjcbhg_ord v2exdfdshg_ord v2exdfvthg_ord v2exdfpphg_ord v2clrspct v2stcritrecadm v2elfrfair_ord v2exagehos v2exagehog
ren country_name Country
ren year Year
ren v2x_polyarchy polyarchy
ren v2x_liberal liberal
ren v2x_partip partip
ren v2xdl_delib delib
ren v2x_egal egal

egen country_id = group(Country)
xtset country_id Year

** Democracy index

egen democracy_index = rowmean(delib egal liberal partip polyarchy)
lab var democracy_index "Democracy index"
notes democracy_index : Source - V-Dem

** Checks and balances index

gen checks_balances_index = (v2x_jucon+v2xlg_legcon)/2
gen lag_checks_balances_index = L.checks_balances_index
lab var checks_balances_index "Checks and balances index"
lab var lag_checks_balances_index "Checks and balances index (1 lag)"
notes checks_balances_index : Source - V-Dem
notes lag_checks_balances_index : Source - V-Dem

** Free and fair election dummy
gen election_freefair = .
replace election_freefair = 1 if v2elfrfair_ord==3 | v2elfrfair_ord==4
replace election_freefair = 0 if v2elfrfair_ord==0 | v2elfrfair_ord==1 | v2elfrfair_ord==2
lab var election_freefair "Free and fair elections"
drop v2elfrfair_ord

** Measures of the power of the HOS/HOG

// HOS forms of power
gen hos_disleg=v2exdfdshs_ord/3
gen hos_appointcab=v2exdfcbhs_ord/4
gen hos_discab=v2exdfdmhs_ord/3
gen hos_veto=v2exdfvths_ord/4        
gen hos_propose=v2exdfpphs_ord
recode hos_propose (0=2) (2=0) //We reorder so that 0 corresponds to less power
replace hos_propose=hos_propose/2

//HOG forms of power
gen hog_disleg=v2exdjdshg_ord/3
gen hog_appointcab=v2exdjcbhg_ord/2
gen hog_discab=v2exdfdshg_ord/3
gen hog_veto=v2exdfvthg_ord/4
gen hog_propose=v2exdfpphg_ord
recode hog_propose (0=2) (2=0) //We reorder so that 0 corresponds to less power
replace hog_propose=hog_propose/2

** Leader age
ren v2exagehos age_hos
ren v2exagehog age_hog
foreach k in hos hog {
	replace age_`k' = . if age_`k'==99
	replace age_`k' = Year - age_`k'
	replace age_`k' = . if age_`k'<0 | age_`k'>100
}


** Aggregate indices
egen hos_power_linear = rmean(hos_disleg hos_appointcab hos_discab hos_veto hos_propose)
egen hog_power_linear = rmean(hog_disleg hog_appointcab hog_discab hog_veto hog_propose)
lab var hos_power_linear "HOS power index"
lab var hog_power_linear "HOG power index"

** Bureaucracy
ren v2clrspct impartial_admin
ren v2stcritrecadm burocrat_merit
egen bureaucracy_index = rowmean(impartial_admin burocrat_merit)
lab var impartial_admin "Impartial public administration"
lab var burocrat_merit "Meritoracy in administration"
lab var bureaucracy_index "Bureaucracy quality index"
notes impartial_admin : Source - V-Dem
notes burocrat_merit : Source - V-Dem
notes bureaucracy_index : Source - V-Dem

** Harmonizing country names and saving output

keep Country Year democracy_index checks_balances_index lag_checks_balances_index v2elparlel v2x_regime hos_power_linear hog_power_linear impartial_admin burocrat_merit bureaucracy_index election_freefair age_hos age_hog

replace Country="Bosnia-Herz"  if Country=="Bosnia and Herzegovina"
replace Country="Myanmar"  if Country=="Burma/Myanmar"
replace Country="C. Verde Is."  if Country=="Cape Verde"
replace Country="Cent. Af. Rep."  if Country=="Central African Republic"
replace Country="PRC"  if Country=="China"
replace Country="Comoro Is."  if Country=="Comoros"
replace Country="Czech Rep."  if Country=="Czech Republic"
replace Country="Congo (DRC)"  if Country=="Democratic Republic of the Congo"
replace Country="Dom. Rep."  if Country=="Dominican Republic"
replace Country="Eq. Guinea"  if Country=="Equatorial Guinea"
replace Country="Swaziland"  if Country=="Eswatini"
replace Country="GDR"  if Country=="German Democratic Republic"
replace Country="FRG/Germany"  if Country=="Germany"
replace Country="PRK"  if Country=="North Korea"
replace Country="Macedonia"  if Country=="North Macedonia"
replace Country="P. N. Guinea"  if Country=="Papua New Guinea"
replace Country="Congo"  if Country=="Republic of the Congo"
replace Country="Solomon Is."  if Country=="Solomon Islands"
replace Country="S. Africa"  if Country=="South Africa"
replace Country="Korea, Republic of"  if Country=="South Korea"
replace Country="Gambia"  if Country=="The Gambia"
replace Country="Trinidad-Tobago"  if Country=="Trinidad and Tobago"
replace Country="UAE"  if Country=="United Arab Emirates"
replace Country="UK"  if Country=="United Kingdom"
replace Country="USA"  if Country=="United States of America"
drop if Country=="Baden"
drop if Country=="Bavaria"
drop if Country=="Brunswick"
drop if Country=="Country"
drop if Country=="Hamburg"
drop if Country=="Hanover"
drop if Country=="Hesse-Darmstadt"
drop if Country=="Hesse-Kassel"
drop if Country=="Hong Kong"
drop if Country=="Mecklenburg Schwerin"
drop if Country=="Modena"
drop if Country=="Nassau"
drop if Country=="Oldenburg"
drop if Country=="Papal States"
drop if Country=="Parma"
drop if Country=="Piedmont-Sardinia"
drop if Country=="Republic of Vietnam"
drop if Country=="Saxe-Weimar-Eisenach"
drop if Country=="Saxony"
drop if Country=="Tuscany"
drop if Country=="Two Sicilies"
drop if Country=="Würtemberg"
drop if Country=="Zanzibar"
drop if Country=="Palestine/Gaza"
drop if Country=="Palestine/British Mandate" & Year>1947
drop if Country=="Palestine/West Bank" & Year<=1948
replace Country="Occupied Palestinian Territories"  if Country=="Palestine/British Mandate"
replace Country="Occupied Palestinian Territories"  if Country=="Palestine/West Bank"

keep if Year>=1940

save "$project_path/data/2_intermediary/outcomes/outcomes_vdem.dta", replace
